package com.verisette.ncos.service.card.bizunit;

import java.sql.Timestamp;

import org.apache.log4j.Logger;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bizunit.CommonBusinessUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;

import com.verisette.ncos.persistence.dao.TransCustomerOrderDAO;
import com.verisette.ncos.persistence.domain.MasterMember;
import com.verisette.ncos.persistence.domain.MasterTemplate;
import com.verisette.ncos.persistence.domain.MasterUserProfile;
import com.verisette.ncos.persistence.domain.TransCustomerOrder;
import com.verisette.ncos.web.card.bean.CardCreateBean;

public class InsertTransCustomerOrderBU extends CommonBusinessUnit {
	private Logger logger = Logger.getLogger(InsertTransCustomerOrderBU.class);
	
	TransCustomerOrderDAO transCustomerOrderDAO;
	@Required
	@Autowired
	public void setTransCustomerOrderDAO(TransCustomerOrderDAO transCustomerOrderDAO) {
		this.transCustomerOrderDAO = transCustomerOrderDAO;
	}
	
	
	@Override
	public void execute(ProcessContext processContext) throws ProcessException {
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": execute" );

		MasterUserProfile userBean = (MasterUserProfile) processContext.getUserBean();
		TransCustomerOrder order = null;
		
		Object o = processContext.getOriginalRequest().getValueObject();
		if(o instanceof CardCreateBean){
			CardCreateBean cardCreateBean = ( (CardCreateBean) o);
			order = cardCreateBean.getOrder(); 
			
			MasterMember member = cardCreateBean.getOrder().getMasterMember();
			order.setMasterMember(member);
			
			MasterTemplate template = cardCreateBean.getOrder().getMasterTemplate();
			order.setMasterTemplate(template);
		} else if(o instanceof MasterMember){
			order = (TransCustomerOrder) o;
		}

		order.setCreatedUspId(userBean.getUspId());
		order.setCreatedBy(userBean.getUsername());
		if(order.getCreatedDate()==null){
			order.setCreatedDate(new Timestamp(System.currentTimeMillis()));
		}
		order.setStatus(0);//0=Waiting for print
		order = transCustomerOrderDAO.insertTransCustomerOrder(order);
		
		processContext.setValueObject(order);
	}
}